package com.citymanage.demo.controller.PersonController;

import com.citymanage.demo.po.person.Person;
import com.citymanage.demo.po.worker.Worker;
import com.citymanage.demo.service.cases.CaseService;
import com.citymanage.demo.service.manager.SelectStall;
import com.citymanage.demo.service.person.PersonManageService;
import com.citymanage.demo.service.person.SelectPersonal;
import com.citymanage.demo.service.worker.WorkerManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RequestMapping("/person")
@Controller
public class PersonController {
    public Integer personCurrentID;
    @Autowired
    PersonManageService personManageService;
    @Autowired
    SelectPersonal selectPersonal;
    @Autowired
    SelectStall selectStall;
    @Autowired
    CaseService caseService;
    @Autowired
    WorkerManageService workerManageService;
    @RequestMapping("/reportworker" )
    public String showWorkerTableComplete(Model model){
        model.addAttribute("users",workerManageService.selectWorker());
        return "person/reportworker";
    }
    @RequestMapping("/stallsinformation" )
    public String showWorkerTableBasic(Model model){

        model.addAttribute("select",selectStall.selectAll());
        return "person/stallsinformation";
    }
    @RequestMapping("/evaluatecase" )
    public String showWorkerFormValidate(Model model){
        model.addAttribute("cases",caseService.selectBykey(personCurrentID));
        return "person/evaluatecase";
    }
    @RequestMapping("/submitcase" )
    public String showWorkerFormBasic(){
        return "person/submitcase";
    }
    @RequestMapping("")
    public String showWorkerLogin(Model model){
        final Map map = model.asMap();
        for (final Object obj : map.keySet()) {
            personCurrentID = (Integer) map.get(obj);
        }
        return "person/person";
    }
    @RequestMapping("/info")
    public String showWorkerInfo(Model model){
        model.addAttribute("users",selectPersonal.find(personCurrentID));
        return "person/info";}
    @RequestMapping("/change_selfinfo")
    public String showWorkerChangeSelfInfo(){return "person/change_selfinfo";}




    @RequestMapping(value = "/updatePerson", method = RequestMethod.POST)
    public String updateWorker(@RequestParam("account") String account, @RequestParam("phone") String phone, @RequestParam("password1") String password1, @RequestParam("password2") String password2, HttpSession session, Model model) {
        if (password1.equals(password2)) {
            Person person = personManageService.updatePerson(personCurrentID,account ,phone, password1);
            if (person != null) {
                return "index";
            } else return "index";
        } else {

            return "index";
        }
    }


    @RequestMapping(value = "/submitCase",method =RequestMethod.POST )
    public String submitCase(@RequestParam("clocation")String clocation,@RequestParam("ctime")String ctime,@RequestParam("cdescribe") String cdescribe){
        System.out.println(cdescribe);
        caseService.submitCase(clocation,ctime,cdescribe,personCurrentID);
        return "redirect:/person/submitcase";
    }
    @RequestMapping(value = "/scoreCase",method =RequestMethod.POST )
    public String scoreCase(@RequestParam("id")String id,@RequestParam("score")String score,@RequestParam("wid")String wid){
        Integer key=Integer.valueOf(wid);
        Worker worker=workerManageService.selectScore(key);
        Integer cscore=Integer.valueOf(score);
        Integer cid=Integer.valueOf(id);
        Integer oldScore=0;
        if(worker.getWscore()==null) {
            workerManageService.updateWscore(key,cscore);
        }
        else {
            oldScore = worker.getWscore();
            Integer newScore = oldScore + cscore;
            workerManageService.updateWscore(key,newScore);
        }
        caseService.scoreCase(cid,cscore);

        return "redirect:/person/evaluatecase";
    }

    @RequestMapping("/homePage")
    public String showhomePage(Model model){
        model.addAttribute("workers",workerManageService.workerScore());
        return "/person/homePage";
    }
}
